﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class EquipmentInformation : EFViewCmes
    {
        public EquipmentInformation(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    this.uploadArea.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            if (this.upload.IsBusy)
            {
                this.ValidateMessagePopup("文件正在上传中，请稍候再提交数据！");//弹出提示信息                
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIInfo eiInfo = this.BuildSubmitEIInfo(eServiceName, Mode.ToString().ToLower(), EIConstants.ResultBlock);
            Dictionary<string, string> dic = upload.InsertFileArgs;
            EIBlock inquBlock = eiInfo.AddBlock("insertfiles");
            EIColumn dir = new EIColumn();
            dir.Name = "fdKeyId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdMemoryName";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdForeignId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            foreach (string obj in dic.Keys)
            {
                EIRow row = new EIRow();
                row.Add("fdKeyId", dic[obj]);
                row.Add("fdMemoryName", obj);
                row.Add("fdForeignId", "");
                inquBlock.AddRow(row);
            }
            base.Submit(eiInfo, DoCommitCompleted);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }


        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
